using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// Summary description for HitCountClass
/// </summary>
public partial class SQLServerClass
{
     	 
    public void WriteToSQL(long HitCounter)
	{
        try
        {
            SqlConnection sqlConnection =
            new SqlConnection(connectionString);
            SqlCommand sqlCommand = new SqlCommand(
            "Update tblHitCounter set HitCounter="
            + HitCounter + ",LastUpdate=getdate() "+
           " where Name='Site'"
            ,sqlConnection);
            sqlConnection.Open();
            sqlCommand .ExecuteNonQuery();
            sqlConnection.Close();
            sqlConnection.Dispose();
        }
        catch (Exception ex)
        {
            errorMessage= ex.Message;
        }
	}
    public int ReadFromSQL()
    {
        int HitCounter=0;
        try
        {
            SqlConnection sqlConnection =
            new SqlConnection(connectionString);
            SqlCommand sqlCommand = new SqlCommand(
            "select HitCounter "+
            " from tblHitCounter " +
           " where Name='Site'"
            , sqlConnection);
            sqlConnection.Open();
            SqlDataReader sqlDataReader=
            sqlCommand.ExecuteReader();
            if (sqlDataReader.Read())
            {
                HitCounter = sqlDataReader.GetInt32(0);
               
            }
            sqlDataReader.Close();
            sqlConnection.Close();
            sqlConnection.Dispose();
        }
        catch (Exception ex)
        {
            HitCounter=1000;
            errorMessage=ex.Message;
        }
        return HitCounter;
    }
}
